<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * user_interface
 *
 * @author Simon Emms <simon@simonemms.com>
 */
class user_interface {
    
    
    protected
        $_arrCSS = array(),
        $_arrJS = array(),
        $_arrUI = array(),
        $_theme;
    
    const theme = '%theme%';
    
    
    /**
     * Constructor
     * 
     * @todo Detect available themes 
     */
    public function __construct() {
        /* Fetch the theme */
        $objCI = &get_instance();
        
        $objCI->load->config('user_interface');
        
        $theme = $objCI->config->item('theme', 'user_interface');
        
        /* Check the string is set - default otherwise */
        if(!is_string($theme)) { $theme = 'default'; }
        
        /* Set to default */
        $this->_theme = $theme;
    }
    
    
    
    /**
     * Add HTML
     * 
     * Do we need to add the HTML to the
     * UI
     * 
     * @return bool
     */
    protected function _add_html($string) {
        return !(bool) preg_match('/^\</', $string);
    }
    
    
    
    /**
     * Parse Extension
     * 
     * Adds the extension to the file
     * 
     * @param string $file
     * @param string $extension
     * @return string
     */
    protected function _parse_extension($file, $extension) {
        if($this->_add_html($file)) {
            $extension = preg_replace('/^(\.)?(.+)/', '.\\2', $extension);

            /* Add the extension */
            $regex_extension = preg_quote($extension, '/');
            
            if(!preg_match('/('.$regex_extension.')$/', $file)) {
                $file .= $extension;
            }
        }
        
        return $file;
    }
    
    
    /**
     * Parse Location
     * 
     * Work out the file location
     * 
     * @param string $location
     * @return string
     */
    protected function _parse_location($location, $add_mobile = false) {
        if(!preg_match('/'.preg_quote(INTERFACE_DIR, '/').'/', $location) && !preg_match('/^(http(s)?|ftp|\/\/)/i', $location)) {
            $objCI = &get_instance();
            
            $dir = INTERFACE_DIR.$this->_theme.'/';
            $dir .= $location;
            return $dir;
        } else {
            return $location;
        }
    }
    
    
    
    /**
     * Parse Theme
     * 
     * Add the theme to the string
     * 
     * @param string $location
     * @return string
     */
    protected function _parse_theme($location) {
        return str_replace(self::theme, $this->_theme, $location);
    }
    
    
    /**
     * CSS
     * 
     * Adds a CSS file
     * 
     * @param string $location 
     */
    public function css($location) {
        $objCI = &get_instance();
        
        $location1 = $this->_parse_extension($location, 'css');
        
        $location1 = $this->_parse_location($location1, true);
        
        $location1 = $this->_parse_theme($location1);
        
        if($objCI->config->item('is_mobile') && file_exists(remove_slash(FCPATH.$location1)) === false) {
            /* Mobile site and CSS does NOT exists - try the ordinary file */
            $location = $this->_parse_extension($location, 'css');
        
            $location = $this->_parse_location($location, false);

            $location = $this->_parse_theme($location);
        } else {
            $location = $location1;
        }
        
        if(file_exists(remove_slash(FCPATH.$location)) || $this->_add_html($location) === false) {
            if($this->_add_html($location)) {
                $location = '<link rel="stylesheet" type="text/css" href="'.interface_url($location, null, false, false).'" />';
            }
            $this->_arrCSS[] = $location;
        }
    }
    
    
    
    /**
     * Get theme
     * 
     * Returns the theme name
     * 
     * @return string
     */
    public function get_theme() { return $this->_theme; }
    
    
    
    /**
     * JS
     * 
     * Adds a JS file
     * 
     * @param string $location 
     */
    public function js($location) {
        $location = $this->_parse_extension($location, 'js');
        
        $location = $this->_parse_location($location);
        
        $location = $this->_parse_theme($location);
        
        $fqd = (bool) preg_match('/^((http(s)?)|(ftp)\:\/\/)/', $location);
        
        if(file_exists(remove_slash(FCPATH.$location)) || $this->_add_html($location) === false || $fqd) {
            if($this->_add_html($location)) {
                $url = $fqd ? $location : interface_url($location, null, false, false);
                $location = '<script type="text/javascript" src="'.$url.'"></script>';
            }
            $this->_arrJS[] = $location;
        }
    }
    
    
    /**
     * Output
     * 
     * Prepare the output
     * 
     * @return string
     */
    public function output() {
        
        $arrTypes = array(
            'css', 'js', 'ui',
        );
        
        $output = '';
        
        foreach($arrTypes as $type) {
            $type = '_arr'.strtoupper($type);
            
            if(isset($this->$type) && is_array($this->$type) && count($this->$type)) {
                foreach($this->$type as $array) {
                    $output .= $array."\n\t";
                }
            }
        }
        
        return $output;
        
    }
    
    
    /**
     * UI
     * 
     * Add a general UI tag
     * 
     * @param string $location
     */
    public function ui($location) {
        
        $location = $this->_parse_theme($location);
        
        $this->_arrUI[] = $location;
        
    }
    
    
}

?>